package tv.danmaku.ijk.media.exo.demo.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Pair;
import android.view.Display;
import android.view.WindowManager;

import java.lang.reflect.Method;

public class ScreenResolution {
    public ScreenResolution() {
    }

    public static Pair<Integer, Integer> getResolution(Context ctx) {
        return Build.VERSION.SDK_INT >= 17 ? getRealResolution(ctx) : getRealResolutionOnOldDevice(ctx);
    }

    private static Pair<Integer, Integer> getRealResolutionOnOldDevice(Context ctx) {
        try {
            WindowManager wm = (WindowManager)ctx.getSystemService("window");
            Display display = wm.getDefaultDisplay();
            Method mGetRawWidth = Display.class.getMethod("getRawWidth");
            Method mGetRawHeight = Display.class.getMethod("getRawHeight");
            Integer realWidth = (Integer)mGetRawWidth.invoke(display);
            Integer realHeight = (Integer)mGetRawHeight.invoke(display);
            return new Pair(realWidth, realHeight);
        } catch (Exception var7) {
            DisplayMetrics disp = ctx.getResources().getDisplayMetrics();
            return new Pair(disp.widthPixels, disp.heightPixels);
        }
    }

    @TargetApi(17)
    private static Pair<Integer, Integer> getRealResolution(Context ctx) {
        WindowManager wm = (WindowManager)ctx.getSystemService("window");
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        return new Pair(metrics.widthPixels, metrics.heightPixels);
    }
}
